#include "pch.h"
#include "logging.h"

#ifdef _USE_LOG4CXX

#elif _USE_SPDLOG
spdlog_instance& spdlog_instance::get_instance()
{
	static spdlog_instance glog;
	return glog;
}

std::string spdlog_instance::get_name()
{
	return "glog";
}

std::shared_ptr<spdlog::logger> spdlog_instance::get_logger()
{
	std::shared_ptr<spdlog::logger> logger = spdlog::get(get_name());
	return logger;
}

std::shared_ptr<spdlog::logger> spdlog_instance::get_console_logger()
{
	std::shared_ptr<spdlog::logger> logger = spdlog::get("console");
	return logger;
}

void spdlog_instance::init(const char * file)
{
	//spdlog::set_async_mode(4096);
	spdlog::set_sync_mode();
	spdlog::set_level(spdlog::level::debug);
	spdlog::set_pattern("%Y%m%d %H:%M:%S.%e [%l][%t] %v");

	if (!spdlog::get("console"))
	{
		spdlog::stdout_logger_mt("console");
	}
	if (!spdlog::get(spdlog_instance::get_name()))
	{
		time_t lt = time(0);
		struct tm* pTm = localtime(&lt);
		char szTimeStamp[32] = { 0 };
		strftime(szTimeStamp, sizeof(szTimeStamp), "%Y%m%d", pTm);
		char szLogFileName[256] = { 0 };
		sprintf(szLogFileName, "%s.log", szTimeStamp);
		try
		{
			spdlog::rotating_logger_mt(get_name(), szLogFileName, 1024 * 1024 * 50, 3);
		}
		catch (const spdlog::spdlog_ex& e)
		{
			try
			{
				int i = 1;
				do
				{
					sprintf(szLogFileName,"%s_%d.log",szTimeStamp,i);
					if (i++ >=100)
					{
						break;
					}
				} while (!spdlog::rotating_logger_mt(get_name(), szLogFileName, 1024 * 1024 * 50, 3));
			}
			catch (...)
			{

			}
		}
	}
}


spdlog_instance::spdlog_instance()
{

}

spdlog_instance::~spdlog_instance()
{

}

void spdlog_instance::uninit()
{
	spdlog::drop(spdlog_instance::get_name());
}



#else
void console_log(const std::string& slog)
{
	std::cout << slog;
}
#endif




